package org.xqyy.eval.service.impl;

import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.xqyy.eval.dao.*;
import org.xqyy.eval.service.IPatientService;
import org.xqyy.eval.util.DataFormatUtil;

import java.util.*;

/**
 * Created by luoxiaoming on 16-12-27.
 */

@Component
@Transactional(readOnly = true)
public class PatientService implements IPatientService {

    @Autowired
    private IPatientDao patientDao;
    @Autowired
    private ITestPaperDao testPaperDao;
    @Autowired
    private IQuestionDao questionDao;
    @Autowired
    private IExternalEvalMapperDao externalEvalMapperDao;

    @Autowired
    private IExternalEvalRecordDao externalEvalRecordDao;


    @Override
    @Transactional(isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED, readOnly = false)
    public void add(Map<String, Object> params) {
        patientDao.add(params);
    }

    @Override
    @Transactional(isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED, readOnly = false)
    public void modify(Map<String, Object> params) {
        patientDao.modify(params);
    }

    @Override
    @Transactional(isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED, readOnly = false)
    public void delete(Map<String, Object> params) {
        patientDao.delete(params);
    }

    @Override
    public Map<String, Object> loadOne(Map<String, Object> params) {
        return patientDao.loadOne(params);
    }

    @Override
    public List<Map<String, Object>> load(Map<String, Object> params) {
        return patientDao.load(params);
    }

    @Override
    public void eval(Map<String, Object> res) throws Exception {
        Map<String, Object> patient = patientDao.loadOne(res);
        if (patient == null) {
            throw new RuntimeException("该病人未有当前科室的就诊记录!");
        } else {
            res.put("deptCode", patient.get("visitDept"));
            String month = DataFormatUtil.defectDateFormat.format(new Date());
            Map<String, Object> tempParams = new HashMap();
            tempParams.put("targetDeptCode", res.get("deptCode"));
            tempParams.put("month", month);
            tempParams.put("patientId",patient.get("patientId"));
            int sumCount = externalEvalRecordDao.loadOreadyFillCount(tempParams);
            if (sumCount>0){
                throw new RuntimeException("提示：当前患者已经对本次就诊进行过评价!");
            }
            List<Map<String, Object>> evalMapperList = externalEvalMapperDao.load(res);
            if (evalMapperList != null && evalMapperList.size() > 0) {
                Map<String, Object> externalEvalMapper = evalMapperList.get(0);
                Map<String, Object> paper = testPaperDao.loadOne(externalEvalMapper);
                List<Map<String, Object>> paperDetail = questionDao.load(paper);
                paper.put("detail", paperDetail);
                res.put("deptName", patient.get("visitDeptName"));
                res.put("paper", paper);
            } else {
                throw new RuntimeException("该科室未定义问卷，请与管理员联系!");
            }
        }
    }

    @Override
    @Transactional(isolation = Isolation.DEFAULT, propagation = Propagation.REQUIRED, readOnly = false)
    public void fillQuestions(Map<String, Object> params) {
        params.put("deptCode", params.get("targetDeptCode"));
        List<Map<String, Object>> evalMapperList = externalEvalMapperDao.load(params);
        String month = DataFormatUtil.defectDateFormat.format(new Date());
        params.put("month", month);
        String id = UUID.randomUUID().toString();
        params.put("id", id);
        params.put("generatedTime", new Date());
        params.put("status", "0");
        int sumCount = externalEvalRecordDao.loadOreadyFillCount(params);
        if (sumCount>0){
            throw new RuntimeException("提示：当前患者已经对本次就诊进行过评价!");
        }
        List<Map<String, Object>> detail = (List<Map<String, Object>>) params.get("detail");
        int score = 0;
        for (Map<String, Object> el : detail) {
            int tempScore = 0;
            el.put("masterId", id);
            String val = (String) el.get("grade");
            if (val.equals("A")) {
                tempScore = 4;
            } else if (val.equals("B")) {
                tempScore = 8;
            } else if (val.equals("C")) {
                tempScore = 12;
            } else if (val.equals("D")) {
                tempScore = 16;
            } else if (val.equals("E")) {
                tempScore = 20;
            }
            score = score + tempScore;
            el.put("grade", tempScore);
            externalEvalRecordDao.addDetail(el);
        }
        params.put("score", score);
        externalEvalRecordDao.add(params);

    }
}
